package com.thoughtworks.commonlibrary.compat.Image;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestFutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * desc   : 图片加载的包装器
 * author : guowenlong
 * Email  : guowenlong20000@gmail.com
 */
public class ImageLoader {
    @SuppressLint("CheckResult")
    public static void imageLoad(Context context, String url, ImageView iv, @DrawableRes int errorImage) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(errorImage).error(errorImage);
        Glide.with(context).load(url).apply(requestOptions).into(iv);
    }

    @SuppressLint("CheckResult")
    public static void backgroundLoad(Context context, String url, View iv, @DrawableRes int errorImage) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(errorImage).error(errorImage);
        Glide.with(context).load(url).apply(requestOptions).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                iv.setBackgroundDrawable(resource);
            }
        });
    }
}